// Copyright 2024 Tvux
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package _014

func longestCommonPrefix(strs []string) string {
	s0 := strs[0]
	for i := range s0 {
		for _, str := range strs {
			if len(str) == i || str[i] != s0[i] {
				return s0[:i]
			}
		}
	}
	return s0
}

// longestCommonPrefix
// 弄了一个二维数组来存放strs的每个字符串的每个字符，然后竖着判断
// f l o w e r
// f l o w
// f l i g h t
//func longestCommonPrefix(strs []string) string {
//	s := make([][]byte, len(strs))
//	minLen := len(strs[0])
//	for i, str := range strs {
//		s[i] = []byte(str)
//		minLen = min(minLen, len(s[i])) // 最长公共前缀的长度肯定不超过最短的那个字符串的长度
//	}
//	i := 0
//	for ; i < minLen; i++ {
//		c := s[0][i]
//		for j := 1; j < len(s); j++ {
//			if s[j][i] != c {
//				return strs[0][:i]
//			}
//		}
//	}
//	return strs[0][:i]
//}
